<?php
define('ALLOW_INCLUDE', true);
include_once("includes/common.php");
include_once("class/User.class.php");
include_once("class/Explore.class.php");
include_once("class/Action.class.php");
require_once("islogin.php");


$pageName = "笔记详情";
$explore = new Explore();
$tags_array = json_decode($explore->randomTags(1),true);

$nid = $_GET["id"];
if (!$nid){
    header("location: /");
    return;
}
$expinfo = json_decode($explore->getIdNotes($nid),true);
if (!$expinfo){
    header("location: /");
    return;
}
$exploreTitle = $expinfo["title"];
$avatar = !$expinfo["avatar"] ? "data/avatar/default.jpg" : $expinfo["avatar"];
$expImage = explode(",",$expinfo["image"]);
$imageNum = count($expImage);

$isfans = $explore->getIsFans($expinfo['user_id'],$user_info["message"]["user_id"]);
if($islogin!=1){
    $isfans = false;
}
//var_dump($isfans);
$islike = json_decode($explore->getLikeAndFav($expinfo['note_id'],$user_info["message"]["user_id"]),true);
$note_statistics = json_decode($explore->getNoteStatistics($nid),true);

$qrCodeUrl = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

if($expinfo['is_view']!=1){
    $delete_note = 1;
    include 'no_view.php';
    return;
}
if($expinfo['check_result']!=1){
    $check_result = $expinfo['check_result'];
    include 'no_view.php';
    return;
}
if($user_info["message"]["user_id"]){
    $action = new Action();
    $action->addViewAction($user_info["message"]["user_id"],$expinfo['note_id']);  //加入浏览记录
}
?>
<!DOCTYPE html>
<html>
<?php require_once('head.php'); ?>
	<body>
    <?php require_once('header.php'); ?>
		<div class="container-fluid content">
			<div class="row">
                <?php require_once('menu.php'); ?>
				<div class="col watercon">
					<div class="row justify-content-center explore_content">
                            <!--视频笔记-->
                            <div class="col exp_left_content" <?php if ($expinfo["note_type"]==0){echo 'style="display:none"';}?>>
                                <img src="static/img/exp-playvideo.svg" class="start_video" id="startVideoImg">
                                <video id="noteVideo" class="note_video" preload="none" poster="<?php echo getFirstValidImage($expinfo["image"]); ?>">
                                    <source data-src="<?php echo getMp4Files($expImage); ?>" type="video/mp4" />
                                </video>
                            </div>
						<!--图文笔记-->
						<div class="col exp_left_content"  <?php if ($expinfo["note_type"]==1){echo 'style="display:none"';}?>>
							<div id="carouselExampleControls" class="carousel slide">
								<div class="carousel-inner">
                                    <?php if(!getMp4Files($expImage)){?>
                                        <?php for ($i = 0; $i < $imageNum; $i++) {$activeClass = ($i === 0) ? ' active' : '';?>
                                            <div class="carousel-item<?php echo $activeClass; ?>">
                                                <img src="<?php echo $expImage[$i]; ?>" class="content_pic d-block w-100 zoomable" />
                                            </div>
                                        <?php }?>
                                    <?php }?>
								</div>
								<!-- Modal for Zoomed Image -->
								<div id="imageModal" class="modal">
									<span class="close"><img src="static/img/close.svg" alt="" /></span>
									<img class="modal-content" id="imgZoom">

									<button class="carousel-control-prev" type="button"
										data-bs-target="#carouselExampleControls" data-bs-slide="prev">
									</button>
									<button class="carousel-control-next" type="button"
										data-bs-target="#carouselExampleControls" data-bs-slide="next">
									</button>
									<!-- 轮播图数量 -->
									<div class="carousel-indicators-container">
										<span id="current-slide-model">1</span> / <span id="total-slides-model"><?php echo $imageNum;?></span>
									</div>

								</div>

							</div>

							<button class="carousel-control-prev" type="button"
								data-bs-target="#carouselExampleControls" data-bs-slide="prev">
							</button>
							<button class="carousel-control-next" type="button"
								data-bs-target="#carouselExampleControls" data-bs-slide="next">
							</button>
							<!-- 轮播图数量 -->
							<div class="carousel-indicators-container">
								<span id="current-slide">1</span> / <span id="total-slides"><?php echo $imageNum;?></span>
							</div>
						</div>
						<div class="col exp_right_content">
							<div class="row explore_head">
								<div class="col-8 uavatar"><img onclick="window.location.href='profile.php?id=<?php echo $expinfo['user_id'];?>'" src="<?php echo $avatar;?>" alt="" /> <span
										class="uname" onclick="window.location.href='profile.php?id=<?php echo $expinfo['user_id'];?>'"><?php echo $expinfo['nickname'];?></span></div>
                                <?php if($user_info["message"]["user_id"]!=$expinfo['user_id']){?>
								<div class="col text-end follow_btn" <?php if($islogin!=1){echo 'onclick="window.location.href=\'/login.php?msg=must\'"';}?>>
                                    <?php if($isfans){?>
                                    <button class="follows_btn" id="followButton" data-fans-id="<?php echo $user_info["message"]["user_id"];?>" data-uid="<?php echo $expinfo['user_id'];?>">已关注</button>
                                    <?php }else{?>
                                    <button id="followButton" data-fans-id="<?php echo $user_info["message"]["user_id"];?>" data-uid="<?php echo $expinfo['user_id'];?>">关注</button>
                                    <?php }?>
                                </div>
                                <?php }?>
							</div>
							<div class="explore_height_fixed">
								<div class="explore_title"><?php echo $exploreTitle;?></div>
								<div class="explore_text" id="exp_nr">
                                    <?php echo $expinfo['content'];?>
                                    <div id="output_tags">
                                        <?php
                                            if($expinfo['tags']){
                                                $tags = explode(',', $expinfo['tags']); // 将标签分开
                                                $formatted_tags = array_map(function($tag) {
                                                    $tag = trim($tag);
                                                    $url = '/?so=' . urlencode($tag);
                                                    return '<a class="note_tags" href="' . $url . '">#' . htmlspecialchars($tag) . '</a>';
                                                }, $tags);
                                                echo implode(' ', $formatted_tags); // 以空格连接标签
                                            }
                                        ?>
                                    </div>
								</div>
								<div class="explore_time">
								    <?php echo formatNoteTime($expinfo['note_time']);?> <?php echo getIpProvince($expinfo['publish_ip']);?>
								    
								    <span class="report_btn" style="float:right;cursor:pointer">举报</span>
								</div>
								<div class="explore_comments_num">共 <?php echo $note_statistics['comments_count'];?> 条评论</div>
                                <!--评论块start-->
                                <!--<div class="row comments_row">
                                    <div class="col-1">
                                        <div class="comments_avatar"><img src="static/testcontenimg/comments.jpg"></div>
                                    </div>
                                    <div class="col right_comments">
                                        <div class="comments_nickname">儒释道
                                            <span class="note_authortag">作者</span>
                                            </div>
                                        <div class="comments_text">666@佛法无边</div>
                                        <div class="comments_time">今天 12:01 上海</div>
                                    </div>
                                </div>-->
                                <!--评论块end-->
                                <div class="notify_end">- THE END -</div>

							</div>
							<div class="row comments_send" <?php if($islogin!=1){echo 'onclick="window.location.href=\'/login.php?msg=must\'"';}?>>
								<div class="col">
									<input class="comments_input" maxlength="300" name="comment_context" type="text" placeholder="说点什么..." />
                                    <?php if ($islike['like']){?>
                                        <span class="exp_btn"><img src="static/img/exp-like-success.svg" alt="" /><span><?php echo $note_statistics['likes_count'];?></span></span>
                                    <?php }else{?>
                                        <span class="exp_btn"><img src="static/img/exp-like.svg" alt="" /><span><?php echo $note_statistics['likes_count'];?></span></span>
                                    <?php }?>

                                    <?php if ($islike['favorite']){?>
                                        <span class="exp_btn"><img src="static/img/exp-star-success.svg" alt="" /><span><?php echo $note_statistics['favorites_count'];?></span></span>
                                    <?php }else{?>
                                        <span class="exp_btn"><img src="static/img/exp-star.svg" alt="" /><span><?php echo $note_statistics['favorites_count'];?></span></span>
                                    <?php }?>
									<span class="exp_btn"><img src="static/img/exp-chat.svg" alt="" /><span id="commentsCount"><?php echo $note_statistics['comments_count'];?></span></span>
									<span class="exp_btn two_exp_btn">
										<!-- 悬停时显示二维码 -->
										<div class="share-pop-container">
											<p class="share_title">扫码分享</p>
											<div class="shadow_qr_code"><img src="/api/qrcode.php?url=<?php echo urlencode($qrCodeUrl);?>" alt="QR Code" class="qr-code" /></div>
											<p class="share_tips">微信扫码分享观看</p>
										</div>
										<img src="static/img/exp-share.svg" alt="" class="share-img" />
										<img src="static/img/link.svg" alt="link" class="link-img" />
									</span>

									<!-- Toast提示框 -->
									<div id="toast" class="toast">复制成功，快去分享给好友吧！</div>
									<button class="sendBtn">发送</button>
									<button class="cancelBtn">取消</button>
								</div>
							</div>
						</div>
					</div>


				</div>
			</div>
		</div>
        <script>
            const uid = '<?php echo $user_info["message"]["user_id"]; ?>';
            const nuid = '<?php echo $expinfo['user_id']; ?>';
            const nid = '<?php echo $expinfo["note_id"]; ?>';
            const avatar = '<?php echo !empty($user_info["message"]["avatar"]) ? $user_info["message"]["avatar"] : '/data/avatar/default.jpg'; ?>';
            const nickname = '<?php echo $user_info["message"]["nickname"]; ?>';
            const iparea = '<?php echo getIpProvince($user_info["message"]["ip"]); ?>';
        </script>
        <script src="static/js/jquery.min.js"></script>
		<script src="static/js/bootstrap.bundle.min.js"></script>
		<script src="static/js/custom.js"></script>
		<script src="static/js/explore.js"></script>
        <script src="static/js/message.min.js"></script>
        <script>
            Qmsg.config({
                timeout:800
            })

        </script>
         <script>
        $(document).ready(function() {
            $(".report_btn").click(function() {
                if(!uid){
                    window.location.href='/login.php?msg=must';
                    return;
                }
                $.ajax({
                    type: "POST",
                    url: "/api/explore.php?type=reportnote",
                    data: {nid: nid},
                    success: function(response) {
                        //console.log(response);
                        Qmsg.success("举报成功！",{
                            position:'center'
                        });
                    }
                });
            });
        });
    </script>
	</body>
</html>